<template>
  <!-- 公司组织树搜索 -->
  <tree-search
    v-bind="$attrs"
    v-on="$listeners"
    isAll
    ref="treeSearch"
    loadList="getOrgsTree"
    emptyDescription="暂无组织机构信息"
    :replaceFields="{ children: 'children', title: 'label', key: 'id' }"
    :api="apiTree"
    @onSelect="onSelect"
    :selectedKeys.sync="selectedKeys"
    :selectOptions.sync="selectOptions"
    :expandedKeySb="expandedKeys"
    :istakeOutHight="istakeOutHight"
    :initExpandAll="initExpandAll"
    :isBranchOpen="isBranchOpen"
  />
</template>

<script>
import TreeSearch from '@/components/businessComponents/TreeSearch' // 树
import { getCustOrgTree, getOrgsTree } from '@/api/subassembly/TreeSelectOrganization' // api
import cloneDeep from 'lodash.clonedeep' // 深拷贝
export default {
  components: {
    TreeSearch
  },
  props: {
    /***
     * 不包含的标签
     */
    orgQuery: {
      type: Object,
      default: () => {}
    },
    // 是否包含虚拟组织结构
    containVirtual: {
      type: Boolean,
      default: false
    },
    // 初始化时展开所有节点
    initExpandAll: {
      type: Boolean,
      default: false
    },
    expandedKeys: {
      type: Array,
      defalut: () => []
    },
    // 是否去掉高度
    istakeOutHight: {
      type: Boolean,
      default: false
    },
    //是否传types
    isTypes: {
      type: Boolean,
      default: false
    },
   //是否展开到分公司
    isBranchOpen: {
      type: Boolean,
      default: false
    }
  },
  computed: {
    // 组织数据
    orgData() {
      return JSON.stringify(this.orgQuery || {}) !== '{}'
        ? this.orgQuery
        : {
            baseOrgType: 0, // 是否基础组织 0否 1是
            containVirtual: 0, //是否包含虚拟组织 0包含 1 不包含
            systemOrgType: 1 //公司组织默认1
            // orgDeptTagCodes: this.GET_ORGLABEL.map((el) => el.code) // 查询指定组织层级下部门，默认全量(不传就是全量)
            // orgDeptTagCode: "CO", // 查询指定组织层级下部门;与orgDeptTagCodes二选一
          }
    },
    // 树节点api
    apiTree() {
      // return { getOrgsTree:()=>getOrgsTree({containVirtual:0}) }
      return {
        getOrgsTree: () => {
          const _this = this
          let type = {}
          if (this.isTypes) {
            type = { types: '1,2,3,7,8' }
          }
          return getCustOrgTree({  ...type, containVirtual: this.containVirtual ? 1 : 0,..._this.orgData })
        }
      }
    }
  },
  data() {
    return {
      selectedKeys: {},
      selectOptions: {}
    }
  },
  methods: {
    // 渲染列表
    getList() {
      return this.$refs.treeSearch.getList()
    },
    onSelect(e) {
      this.$emit('onOrgSelect', e)
    }
  },
  watch: {
    selectedKeys(e) {
      this.$emit('update:selectedKeys', e)
    },
    selectOptions(e) {
      this.$emit('update:selectOptions', e)
    }
  }
}
</script>
